﻿using System;
using System.Linq;
using System.IO;
using SilverlightFileManager;

public partial class DownloadFile : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Get parameters
        string strPortalID = Request.QueryString["PortalID"];
        string strModuleId = Request.QueryString["ModuleId"];
        string strUserID = Request.QueryString["UserID"];
        string strPassword = Request.QueryString["Password"];
        string strFileID = Request.QueryString["FileID"];

        // All parameters must have a value or return nothing
        if (
            strPortalID != "" &&
            strModuleId != "" &&
            strUserID != "" &&
            strPassword != "" &&
            strFileID != ""
            )
        {
            // Convert strings to Integers
            int intPortalID = Convert.ToInt32(strPortalID);
            int intModuleId = Convert.ToInt32(strModuleId);
            int intUserID = Convert.ToInt32(strUserID);
            int intFileID = Convert.ToInt32(strFileID);

            // Check authentication
            string strIPAddress = this.Context.Request.UserHostAddress;
            SilverlightBridgeAuthendicationHeader SilverlightBridgeAuthendicationHeader = new SilverlightBridgeAuthendicationHeader();
            SilverlightBridgeAuthendicationHeader.PortalID = intPortalID;
            SilverlightBridgeAuthendicationHeader.ModuleId = intModuleId;
            SilverlightBridgeAuthendicationHeader.UserID = intUserID;
            SilverlightBridgeAuthendicationHeader.Password = strPassword;
            SilverlightBridgeAuthendicationHeader.IPAddress = strIPAddress;
            Authendication Authendication = new Authendication(SilverlightBridgeAuthendicationHeader);

            // user is valid
            if (Authendication.IsUserValid())
            {
                string strPath = "";
                string strOriginalFileName = "";

                // Get the name and location of the file
                SilverlightFileManagerDALDataContext SilverlightFileManagerDALDataContext = new SilverlightFileManagerDALDataContext();

                using (SilverlightFileManagerDALDataContext)
                {
                    FileUpload_File objSilverlightFile = (from FileUpload_Files in SilverlightFileManagerDALDataContext.FileUpload_Files
                                                          where FileUpload_Files.FileID == intFileID
                                                          where FileUpload_Files.UserID == intUserID
                                                          select FileUpload_Files).FirstOrDefault();

                    strPath = Path.Combine(objSilverlightFile.FilePath, objSilverlightFile.FileName);
                    strOriginalFileName = objSilverlightFile.OriginalFileName; 
                }

                try
                {
                    // Return the file
                    Response.ClearHeaders();
                    Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", strOriginalFileName));

                    Response.ClearContent();
                    Response.ContentEncoding = System.Text.Encoding.UTF8;
                    Response.ContentType = GetContentType(strPath);

                    FileStream sourceFile = new FileStream(strPath, FileMode.Open);
                    long FileSize;
                    FileSize = sourceFile.Length;
                    byte[] getContent = new byte[(int)FileSize];
                    sourceFile.Read(getContent, 0, (int)sourceFile.Length);
                    sourceFile.Close();

                    Response.BinaryWrite(getContent);
                    Response.Flush();
                    Response.Close();

                }
                catch (Exception ex)
                {
                    lblDisplayFilesError.Text = ex.Message;
                }
            }
        }
    }

    #region GetContentType
    public string GetContentType(string strextension)
    {
        string contentType;
        switch (strextension.ToLower())
        {
            case ".gif":
                contentType = "image/gif";
                break;
            case ".jpg":
            case ".jpeg":
                contentType = "image/jpeg";
                break;
            case ".png":
                contentType = "image/png";
                break;
            case ".doc":
                contentType = "application/ms-word";
                break;
            case ".docx":
                contentType = "application/vnd.ms-word.document.12";
                break;
            case ".pdf":
                contentType = "application/pdf";
                break;
            case ".xls":
                contentType = "application/vnd.ms-excel";
                break;
            case ".ppt":
                contentType = "application/vnd.ms-powerpoint";
                break;
            case ".zip":
                contentType = "application/zip";
                break;
            case ".txt":
                contentType = "text/plain";
                break;
            default:
                contentType = "application/octet-stream";
                break;
        }
        return contentType;
    }
    #endregion
}
